<%@ page pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.base.DatabaseObject"%>
<%@page import="com.base.DatabaseDAO"%>
<%@page import="com.base.Config"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="com.utils.LOBs"%>
<%@page import="java.sql.Clob"%>

<%@include file="inc-header.jsp"%><!-- MAIN CONTENT -->
<div class="box bottom10">
	<%@include file="inc-nav-header.jsp"%>
	<%
	String sCatId = Utils.getParam(request, "catid", "0");
	String sId = Utils.getParam(request, "id", "0");
	String sPage = Utils.getParam(request, "page", "1");
	DatabaseObject gameCategory = contentDAO.getContentGroup(new BigDecimal(sCatId));
	DatabaseObject gameContent = contentDAO.getContentVn(new BigDecimal(sId));
	%>
	<div class="tab2">
		<%@include file="dichvu-adv.jsp" %> >>
		<a href="story.jsp?act=dv"> Truyện </a> >>
		<a href="story.jsp?act=dv&catid=<%=gameCategory.getAttribute().get("content_group_id") %>" class="active"> <%=gameCategory.getAttribute().get("NAME") %></a>
	</div>
	<!--end .tab2-->
	<!-- BODY CONTENT -->
	<div class="box clearfix">

		<div class="table">
			<table cellpadding="0" cellspacing="0" border="0" width="100%">
				<tbody>
				<tr>
		                <td width="100%" class="text-left">
		                    <h2 class="clearfix"><a href="#" class="f1"> <strong> <%=gameContent.getAttribute().get("name").toString().replaceAll("(\r\n|\n)", "<br />")%> </strong></a></h2>
		               
		                </td>
		            </tr>
		        </tbody>
		    </table>
		</div><!--end .table-->

		<div class="box-cont">
		    <p class="MsoNormal" style="text-align: justify; line-height: normal;">
		        <span style="font-size: 10pt; font-family: Arial,sans-serif">
		            <%=gameContent.getAttribute().get("data").toString().replaceAll("\n","</br>")%>
		        </span>
		    </p>  
		</div><!--end .box-cont-->
	</div>
	<div class="box clearfix">
		<div class="title">
			<h3>
				<strong>Cùng chuyên mục</strong>
			</h3>
		</div>
	</div>
	<div class="box clearfix">
<%
	int currPage = Integer.parseInt(sPage);
	int rowsPerPage = 7;
	if (!StringTool.isEmptyOrNul(sCatId)) {
		int totalRecs = contentDAO.countContentByCategoryGroup(new BigDecimal(sCatId));
		int numOfPages = (int)((totalRecs % rowsPerPage != 0) ? (totalRecs / rowsPerPage + 1)
				: (totalRecs / rowsPerPage));
		if (totalRecs <= rowsPerPage)
			numOfPages = 1;
		if (totalRecs == 0)
			numOfPages = 0;
		if (currPage > numOfPages)
			currPage = numOfPages;
		if (currPage <= 0)
			currPage = 1;
		long startPos = rowsPerPage * (currPage - 1) + 1;
		long stopPos = startPos + rowsPerPage - 1;
		if (stopPos > totalRecs)
			stopPos = totalRecs;
		long firstPage = 1; // The first page
		long lastPage = numOfPages; // The last page
		long prevPage = currPage - 1;
		if (prevPage < 1) {
			prevPage = 1;
		}
		long nextPage = currPage + 1;
		if (nextPage > numOfPages) {
			nextPage = numOfPages;
		}
		ArrayList<DatabaseObject> gameList = contentDAO.getContentListByCategoryGroup(new BigDecimal(sCatId), currPage, rowsPerPage);
		if (gameList != null && !gameList.isEmpty()) {%>
		<table cellpadding="0" cellspacing="0" border="1" width="100%">
		<%
			for (DatabaseObject game : gameList) {
				if (game.getAttribute().get("content_id").toString().equals(sId)) continue;
			%>
			<tr>
			    <td width="100%" style="vertical-align: top; padding: 0px 2px 5px 4px;">
			        <p><a href="story-detail.jsp??act=dv&catid=<%=sCatId %>&id=<%=game.getAttribute().get("content_id") %>&page=<%=currPage %>"><strong> <%=game.getAttribute().get("name") %> </strong></a></p>
			    </td>
			</tr>
			<%}%>
		</table>
		<%
		}%>
		<div align="center">
			<%
				long startPage = currPage - 3;
				if (startPage < 1) startPage = 1;
				
				long endPage = startPage + 6;
				if (endPage > numOfPages) {
					endPage = numOfPages;
				}
				if (startPage > 3 && endPage - startPage < 6) startPage = endPage - 6;
				if (startPage > 1) {
	        %>
			<a href="story.jsp?act=dv&catid=<%=sCatId %>&page=1">Đầu</a>
			<a href="story.jsp?act=dv&catid=<%=sCatId %>&page=<%=startPage-1 %>"> Trước </a>
			<%}
		        for (long i = startPage; i <= endPage; i++) {
		        	if (currPage == i) {
					out.print("<font style=\"font-weight:bold; font-size: 12px; color: #ff0000;\">" + i + "</font>");
				} else {
	         %>
			<a href="story.jsp?act=dv&catid=<%=sCatId %>&page=<%=i %>" style="font-size: 12px;"> <%=i%></a>
			<%  }
			}
	        if (endPage < numOfPages)
	        {
		    %>
			<a href="story.jsp?act=dv&catid=<%=sCatId %>&page=<%=endPage+1 %>" class="style_paging">Tiếp</a>
			<a href="story.jsp?act=dv&catid=<%=sCatId %>&page=<%=numOfPages %>" class="style_paging">Cuối</a>
			<%}%>
		</div>		
		<%
	}
	%>
	</div>
	
	<div class="box clearfix">
		<div class="title padbot5px">
			<h3>
				<img src="images/icon-story.png" width="23" height="23" alt="" align="left"> <strong> Danh mục truyện</strong>
			</h3>
		</div>
		<!--end .title-->
		<ul class="list-dot"><%
		String catGameParentId = Config.getStringProperty("CAT_STORY_PARENT_ID", "1088", false);
		if (!StringTool.isEmptyOrNul(catGameParentId)) {
			ArrayList<DatabaseObject> gameCategoryList = contentDAO.getContentGroup(catGameParentId, null);
			for (DatabaseObject game : gameCategoryList) {%>
			<li>
				<img src="images/icon-list.png" alt="<%=game.getAttribute().get("NAME") %>">
				<a href="story.jsp?act=dv&catid=<%=game.getAttribute().get("CONTENT_GROUP_ID") %>"> <%=game.getAttribute().get("NAME") %> </a>
			</li>
		<%}}%>
		</ul>
	</div>
	<!-- END BODY CONTENT -->
</div>
<!--end .box bottom10-->
<%@include file="inc-nav-bottom.jsp"%>
<!-- END MAIN CONTENT -->
<%@include file="inc-footter.jsp"%>
